﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Collections.Generic;
using System.Collections.Specialized;


using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.User;

public partial class Theme_3_Register : System.Web.UI.Page
{
    User opUser = new User();
    Department opDepartment = new Department();
    protected  PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected String returnValue;
    LogicServer.NetMeetingClass objNetMeetingClass;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (!IsPostBack)
        {
            InitPage();
            this.btn_AddNew.Attributes.Add("OnClick", "return checkForm()");
            BinderUserLogin();
            btn_out.Text = config.GetLanagerValueByKey("SignOut", PowerCreator.LMS.Language.LanagetItemType.WebTitle);
            
            BinderDepartmentClass();
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass = new LogicServer.NetMeetingClass();
            }

        }
       
    }
   


    protected  void InitPage()
    {

        lbl_InfoTitle_AddStudent.Text = config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Account_loading.Text = config.GetLanagerValueByKey("lbl_AddStudent_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_LoadingName.Text = config.GetLanagerValueByKey("lbl_AddStudent_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_PassWord.Text = config.GetLanagerValueByKey("lbl_AddStudent_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_dbPassWord.Text = config.GetLanagerValueByKey("lbl_AddStudent_DBPassWord", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_BaseInformaition.Text = config.GetLanagerValueByKey("lbl_AddStudent_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddStudent_Name.Text = config.GetLanagerValueByKey("lbl_AddStudent_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Number.Text = config.GetLanagerValueByKey("lbl_AddStudent_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Sex.Text = config.GetLanagerValueByKey("lbl_AddStudent_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_IdNumber.Text = config.GetLanagerValueByKey("lbl_AddStudent_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Born.Text = config.GetLanagerValueByKey("lbl_AddStudent_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Mail.Text = config.GetLanagerValueByKey("lbl_AddStudent_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Class.Text = config.GetLanagerValueByKey("lbl_AddStudent_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Nation.Text = config.GetLanagerValueByKey("lbl_AddStudent_Nation", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Phone.Text = config.GetLanagerValueByKey("lbl_AddStudent_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Native.Text = config.GetLanagerValueByKey("lbl_AddStudent_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Post.Text = config.GetLanagerValueByKey("lbl_AddStudent_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_MobileTelephone.Text = config.GetLanagerValueByKey("lbl_AddStudent_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Address.Text = config.GetLanagerValueByKey("lbl_AddStudent_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_Remark.Text = config.GetLanagerValueByKey("lbl_AddStudent_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Male.Text = config.GetLanagerValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = config.GetLanagerValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_addstudent_selClass.Text = config.GetLanagerValueByKey("lbl_addstudent_selClass", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "* (" + config.GetLanagerValueByKey("theme_Register_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page) + "：1980-01-01)";

        btn_AddNew.Text = config.GetLanagerValueByKey("btn_EditNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = config.GetLanagerValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

       
    }


    /// <summary>
    /// 检查页面数据
    /// </summary>
    /// <returns></returns>
    private bool CheckPageData()
    {
        if (!(txt_UserPwd.Text.ToString() == txt_DBUserPwd.Text.ToString()))
        {
            lab_dbPwd.Text = config.GetLanagerValueByKey("theme_Register_Msg5", PowerCreator.LMS.Language.LanagetItemType.Page) ;
            return false;
        } 

        string search = string.Format("UserName = '{0}'", txt_UserName.Text);
        if (opUser.Count(search) != 0)
        {
            lbl_AddStudent_msg_username.Text = config.GetLanagerValueByKey("theme_Register_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }

        //if (!string.IsNullOrEmpty(txt_Birth.Text))
        //{
        //    if (!TimeUtils.IsDateTime(txt_Birth.Text.Trim()))
        //    {
        //        lab_Birth_css.Text = config.GetLanagerValueByKey("theme_Register_Msg3", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        return false;
        //    }
        //}  
        if (ddl_Class.Items.Count == 0)
        {
            lbl_addstudent_selectdepartment.Text = config.GetLanagerValueByKey("theme_Register_Msg4", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        VOTDepartment vo = opDepartment.Load(int.Parse(ddl_Class.SelectedValue.ToString()));
        if (vo.DepartmentType == 1)
        {
            lbl_addstudent_selectdepartment.Text = config.GetLanagerValueByKey("lbl_addstudent_selectdepartment", PowerCreator.LMS.Language.LanagetItemType.MSG);
            return false;
        }
        return true;
    }

    //private void BinderDepartment()
    //{
    //    string search = "DepartmentType=1";
    //    DataTable dt = opDepartment.Query(search);
    //    ArrayList list = opDepartment.GetDepartmentTree(dt);
    //    ddl_Department.DataSource = list;
    //    ddl_Department.DataTextField = "Text";
    //    ddl_Department.DataValueField = "Value";
    //    ddl_Department.DataBind();
    //}
    private void BinderDepartmentClass()
    {
        //if(ddl_Department.SelectedValue)
       
        string search = "DepartmentType=2 ";
        DataTable dt = opDepartment.Query(search);
        ddl_Class.DataSource = dt;
        ddl_Class.DataTextField = "DepartmentName";
        ddl_Class.DataValueField = "DepartmentId";
        ddl_Class.DataBind();
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string msgWorkOK = "";
        if (!CheckPageData()) return;

        VOTUser voUser = new VOTUser();
        VOTStudent voStudent = new VOTStudent();

        voUser.UserName = txt_UserName.Text;
        voUser.UserPassword = txt_UserPwd.Text;

        //voStudent.StudentAddress = txt_Address.Text;
        //if (!string.IsNullOrEmpty(txt_Birth.Text))
        //    voStudent.StudentBirthday = DateTime.Parse(txt_Birth.Text);

        if (!string.IsNullOrEmpty(ddl_Class.SelectedValue.ToString()))
            voStudent.FKStudentDepartmentId = int.Parse(ddl_Class.SelectedValue.ToString());

        //voStudent.StudentCode = txt_StudentCode.Text;
        //voStudent.StudentDescription = txt_Description.Text;
        //voStudent.StudentEmail = txt_Email.Text;
        //voStudent.StudentHomeplace = txt_Homeplace.Text;
        //voStudent.StudentIdentityCard = txt_Idcard.Text;
        //voStudent.StudentMobileTelephone = txt_Phone.Text;
        //voStudent.StudentName = txt_RealName.Text;
        //voStudent.StudentNation = txt_Nation.Text;
        //voStudent.StudentPost = txt_Post.Text;
        //voStudent.StudentSex = 0;
        voUser.UserType = 0;

        if (radbtn_Female.Checked)
            voStudent.StudentSex = 1;

        //voStudent.StudentTelephone = txt_Tel.Text;
        if (PowerCreator.LMS.Web.Config.WebConfig.IsCheckUserRegister)
        {
            voUser.UserIscheck = 0;
            msgWorkOK = config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkOK_NoIsCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        }
        else
        {
            voUser.UserIscheck = 1;
            msgWorkOK = config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page);
        }

        PowerCreator.LMS.Web.CreatedByInfo objCreatedByInfo = new PowerCreator.LMS.Web.CreatedByInfo();
        objCreatedByInfo.UserID = 999;
        objCreatedByInfo.UserName = txt_UserName.Text;

        int UserID = 0;
        if (opUser.AddStudent(voUser, voStudent, objCreatedByInfo, ref UserID))
        {
            if (PowerCreator.LMS.Web.Config.WebConfig.IsUseNetMeeting)
            {
                objNetMeetingClass.WebAddUser(UserID, voUser.UserName, voUser.UserPassword, (ulong)voUser.UserRule, voUser.UserImageIndex, voStudent.StudentDescription, voStudent.StudentSex, voStudent.StudentTelephone, voUser.UserType);
            }
            
            string url = "Default.aspx";
            //string msgWorkOK=config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page);
            Script.ShowMsgRedirectUrl(msgWorkOK, url);
        }
        else
        {
            string msgWorkNO = config.GetLanagerValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page);
            Script.Alert(this, "msg", msgWorkNO);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "Default.aspx";
        Script.RedirectURL(url);
    }
    protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderDepartmentClass();
    }

    protected void lnkbtn_EN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "1";
    }
    protected void lnkbtn_CN_Click(object sender, EventArgs e)
    {
        Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_LANGEID] = "0";
    }
    protected void BinderUserLogin()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            lal_UserLogin.Text = "<a  href=\"UserLogin.aspx\">" + config.GetLanagerValueByKey("SysManagerLogin", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a> |  <a  href=\"Register.aspx\">" + config.GetLanagerValueByKey("SysManagerRegistor", PowerCreator.LMS.Language.LanagetItemType.Page) + "</a>";
            btn_out.Visible = false;
        }
        else
        {
            PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            lal_UserLogin.Text = login.RealName+ "," + config.GetLanagerValueByKey("Login_msg_code5", PowerCreator.LMS.Language.LanagetItemType.Page) + "！| ";
            btn_out.Visible = true;
        }
    }
    protected void btn_out_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Web.User.User opUser = new PowerCreator.LMS.Web.User.User();
        //opUser.UpdateOnlineUserByUserID(GetLoginEntity.UserID);
        Session.Abandon();
        PowerCreator.LMS.Common.Script.Script.ReloadMe();
    }
}
